package com.bsh.repository;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.AccountLogMapper;
import com.bsh.mybatis.model.Account;
import com.bsh.mybatis.model.AccountLog;

@Repository
public class DaoAccountLog {
	@Resource
	SqlSessionTemplate session;

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	@Resource
	DaoAccount daoAcc;

	AccountLogMapper getMapper() {
		return session.getMapper(AccountLogMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(AccountLog record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(AccountLog record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public AccountLog selectByPrimaryKey(Integer id) {
		AccountLog row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(AccountLog record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(AccountLog record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}

	/**
	 * 
	 * @param accountId
	 *            帐号id
	 * @param sn
	 *            交易明细流水号
	 * @param ip
	 *            操作ip
	 * @return
	 */
	public int addLog(Integer accountId, String sn, String ip) {
		Account account = daoAcc.selectByPrimaryKey(accountId);
		AccountLog log = new AccountLog();
		log.setAccountId(account.getId());
		log.setUserid(account.getUserid());
		log.setMoney(account.getMoney());// 现金
		log.setCloudMoney(account.getCloudMoney());// 商币
		log.setEnabled(account.getEnabled());// 状态
		log.setLastModify(account.getLastModify());// 最后修改时间
		log.setCreateTime(account.getCreateTime());// 创建时间

		log.setSn(sn);//交易明细流水
		log.setIp(ip);// 最后登录ip

		int res = this.insert(log);
		if (res > 0)
			return log.getId();
		return -1;
	}

}
